import org.json.JSONObject;
import org.json.JSONArray;
import org.json.JSONException;
import java.net.URLEncoder;

public class message
{
    public static void revicemsg(final String FromUserName,final String Content){
		//轮循联系人列表取出对应的昵称和备注
		String Name = "";
		try{
			JSONObject json = new JSONObject(Main.Friends);
			JSONArray list = json.getJSONArray("MemberList");
			for(int i = 0;i < list.length();i++){
				json = list.getJSONObject(i);
				String username = json.getString("UserName");
				if(username.equals(FromUserName)){
					if(json.getString("RemarkName").equals("")){
						//没有备注,公众号也没有
						Name = json.getString("NickName");
					}else{
						Name = json.getString("RemarkName");
					}
				}
			}
		}catch (JSONException e){}
		
		System.out.println("收到消息[" + Name + "]:[" + Content + "]");
		
		new Thread(){
			@Override
			public void run(){
				try
				{
					sleep(2000);
				}
				catch (InterruptedException e)
				{}
				//发送消息
				System.out.println(vx.sendtextmsg(Main.userdata.get("skey").toString(),Main.userdata.get("wxuin").toString(),Main.userdata.get("wxsid").toString(),Main.userdata.get("pass_ticket").toString(),Main.userdata.get("username").toString(),FromUserName,URLEncoder.encode(Content)));
			}
		}.start();
	}
	
	public static void sendmsg(String ToUserName,String Content){
		//轮循联系人列表取出对应的昵称和备注
		String Name = "";
		try{
			JSONObject json = new JSONObject(Main.Friends);
			JSONArray list = json.getJSONArray("MemberList");
			for(int i = 0;i < list.length();i++){
				json = list.getJSONObject(i);
				String username = json.getString("UserName");
				if(username.equals(ToUserName)){
					if(json.getString("RemarkName").equals("")){
						//没有备注,公众号也没有
						Name = json.getString("NickName");
					}else{
						Name = json.getString("RemarkName");
					}
				}
			}
		}catch (JSONException e){}

		System.out.println("发送消息[" + Name + "]:[" + Content + "]");
	}
}
